package com.qc31.monitorlib.ui.carreport

import autodispose2.autoDispose
import com.qc31.baselib.auto.AutoDisposeViewModel
import com.qc31.comlib.TimeUtil
import com.qc31.comlib.entity.DayMile
import com.qc31.comlib.entity.Fees
import com.qc31.comlib.entity.PhotoInfo
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import io.reactivex.rxjava3.functions.Consumer
import io.reactivex.rxjava3.subjects.PublishSubject
import java.util.*

/**
 *@Author Jasper
 *@Time   2020/10/15
 *@Desc   车辆模式报表
 */
class ReportItemViewModel : AutoDisposeViewModel() {
    var carId = ""
    var carName = ""

    //时间类型
    var timeType = 4

    //请求数据种类
    var dataType = 0

    //开始时间
    var startTime = ""

    //结束时间
    var endTime = ""

    //费用类型
    var feeType = ""

    //是否隐鲹
    var isHidden = false

    //里程结果监听
    val dayMileSub: PublishSubject<DayMile> by lazy { PublishSubject.create<DayMile>() }

    //照片响应监听
    val photosSub: PublishSubject<ArrayList<PhotoInfo>> by lazy { PublishSubject.create<ArrayList<PhotoInfo>>() }

    //费用监听
    val feeDetailSub: PublishSubject<Fees> by lazy { PublishSubject.create<Fees>() }

    //费用详情监听
    val feeInfoSub: PublishSubject<String> by lazy { PublishSubject.create<String>() }

    val freeEmptySub: PublishSubject<Boolean> by lazy { PublishSubject.create<Boolean>() }

    /**
     * 获取时间
     */
    private fun getTime(day: Int = 0): String {
        return if (day == 0) {

            TimeUtil.instance.getAllTime()
        } else {
            TimeUtil.instance.timeToString(Date().time - day * 24 * 3600 * 1000)
        }
    }

    /**
     * 处理时间
     */
    fun handleTime() {
        when (timeType) {
            1 -> {
                startTime = TimeUtil.instance.getAllTime(TimeUtil.getBeginDayOfYesterday().time)
                endTime = TimeUtil.instance.getAllTime(TimeUtil.getEndDayOfYesterday().time)

            }
            2 -> {
                startTime = TimeUtil.instance.getAllTime(TimeUtil.getDayBegin().time)
                endTime = TimeUtil.instance.getAllTime(TimeUtil.getDayEnd().time)
            }
            3 -> {
                startTime = getTime(2)
                endTime = getTime()
            }
            4 -> {
                startTime = getTime(6)
                endTime = getTime()
            }
        }
        getData()
    }

    /**
     * 获取数据
     */
    private fun getData() {
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return
        }
        toastLoading(true)
        when (dataType) {
            0 -> {
                getMileData()
            }
            3 -> {
                getHistoryPhotos()
            }
            4 -> {
                getFeeDetail()
            }
        }
    }

    /**
     * 查询里程数据
     */
    private fun getMileData() {
        ServiceHelper.getRepository()
            .getDayMile(carId, startTime.substring(0, 8), endTime.substring(0, 8))
            .autoDispose(this)
            .subscribe({
                toastLoading(false)
                if (isHasListData(it.list)) {
                    freeEmptySub.onNext(true)
                    return@subscribe
                }
                freeEmptySub.onNext(false)
                dayMileSub.onNext(it)
            }, error)
    }

    /**
     * 查询照片数据
     */
    private fun getHistoryPhotos() {
        ServiceHelper.getRepository().getHistoryPhotos(carId, startTime, endTime).autoDispose(this)
            .subscribe({
                toastLoading(false)
                if (isHasListData(it)) {
                    freeEmptySub.onNext(true)
                    return@subscribe
                }
                freeEmptySub.onNext(false)
                photosSub.onNext(it)
            }, error)
    }

    /**
     * 查询费用数据
     */
    private fun getFeeDetail() {
        val start = TimeUtil.instance.normal2simple(startTime)
        val end = TimeUtil.instance.normal2simple(endTime)
        ServiceHelper.getRepository()
            .getFeeDetail(carId, TimeUtil.instance.formatHm(start), TimeUtil.instance.formatHm(end))
            .autoDispose(this)
            .subscribe(Consumer {
                toastLoading(false)
                if (isHasListData(it.fees)) {
                    freeEmptySub.onNext(true)
                    return@Consumer
                }
                freeEmptySub.onNext(false)
                feeDetailSub.onNext(it)
            }, error)
    }

    /**
     * 查询费用详情
     */
    fun getFeeInfo(feeType: String) {
        this.feeType = feeType
        ServiceHelper.getRepository().getFeeInfo(
            carId = carId,
            feeType = feeType,
            startTime = TimeUtil.instance.normal2simple(startTime).replace("-", ""),
            endTime = TimeUtil.instance.normal2simple(endTime).replace("-", "")
        ).autoDispose(this).subscribe({
            feeInfoSub.onNext(it.result.toString())
            toastLoading(false)
        }, error)
    }
}
